package com.cssw.fyzb.busi.api.user;

import com.cssw.fyzb.busi.config.ParamConfig;
import com.cssw.fyzb.core.model.vo.user.UserInfoVo;
import com.cssw.fyzb.busi.service.team.TeamService;
import com.cssw.fyzb.busi.service.user.UserService;
import com.cssw.fyzb.core.dao.user.DistrictDao;
import com.cssw.fyzb.core.model.dodb.District;
import com.cssw.fyzb.core.model.dodb.User;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;

/**
 * 获取用户信息
 *
 * @author Feng Chen
 */
@Function(description = "获取用户父类详情", parameters = {
        @Parameter(name = "userId", type = Type.String, description = "用户编号", required = true)
})
@Component
public class GetSupInfo implements Action {

    @Autowired
    private UserService userService;

    @Autowired
    private TeamService teamService;

    @Autowired
    private DistrictDao districtDao;

    @Autowired
    private ParamConfig paramConfig;

    private static final Logger logger = LoggerFactory.getLogger(GetSupInfo.class);

    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");
        try {
            User userOwn = userService.getById(userId);
            Integer num = teamService.getTeamNum(userId);
            UserInfoVo info = new UserInfoVo();
            if (userOwn.getSupUserId() != null) {
                User user = userService.getById(userOwn.getSupUserId());
                UserInfoVo userInfo = new UserInfoVo();
                userInfo.setUserId(user.getUserId());
                userInfo.setSupUserId(user.getSupUserId());
                userInfo.setUserName(user.getUserName());
                userInfo.setNickName(user.getNickName());
                userInfo.setMobile(user.getMobile());
                userInfo.setProvince(user.getProvince());
                userInfo.setCity(user.getCity());
                userInfo.setDistrict(user.getDistrict());

                // 地区名称
                if(user.getDistrict()!=null) {
                    District district = districtDao.getByCode(user.getDistrict());
                    if(district!=null) {
                        userInfo.setDistrict(district.getDistrictName());
                    }
                }
                // 城市名称
                if(user.getCity()!=null) {
                    District district = districtDao.getByCode(user.getCity());
                    if(district!=null) {
                        userInfo.setCity(district.getDistrictName());
                    }
                }
                // 省
                if(user.getProvince()!=null) {
                    District district = districtDao.getByCode(user.getProvince());
                    if(district!=null) {
                        userInfo.setProvince(district.getDistrictName());
                    }
                }

                userInfo.setStatus(user.getStatus());
                userInfo.setSex(user.getSex());
//                userInfo.setIdCard(user.getIdCard());
                if(user.getBirthday()!=null) {
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                    userInfo.setBirthday(formatter.format(user.getBirthday()));
                }
                userInfo.setImsi(user.getImsi());
                userInfo.setFaceAuth(user.getFaceAuth());
                userInfo.setUserLevel(user.getUserLevel());
                userInfo.setTeamLevel(user.getTeamLevel());

                if(user.getCreateTime()!=null) {
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    userInfo.setCreateTime(formatter.format(user.getCreateTime()));
                }
                userInfo.setSign(user.getSign());
               userInfo.setAvatar((user.getAvatar()));
                userInfo.setTeamCount((num!=null)?num:0);
                return Response.success().put("data", userInfo);
            }else{
                UserInfoVo userInfo = new UserInfoVo();
                userInfo.setTeamCount((num!=null)?num:0);
                userInfo.setNickName("无推荐人");
                String defAvatar = paramConfig.getAvatar();
                userInfo.setAvatar(defAvatar);
                return Response.success().put("data", userInfo);
            }
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("获取用户详情失败");
        }
    }

    private Integer getHrn(User user) {
        //实名认证流程需要调整一下：1、三要素认证  2、人脸认证  3、支付认证金额
//        if (!user.isUserAuth()){
//            return 1;
//        }
//        else if (!user.isFaceAuth()){
//            return 2;
//        }
//        else if (!user.isPayAuth()){
//            return 3;
//        }

        return 4;
    }


}
